#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;

void lexical(FILE *fp)
{
    char x = getc(fp);

    num = 0;
    strcpy(id, "\0");
    if(x==' '||x=='\t'||x=='\n')
    {
        while(x==' '||x=='\t'||x=='\n')
        {
            x=getc(fp);
        }
    }
    if(x=='+'||x=='-'||x=='*'||x=='/')
    {
        if(x=='+'){  token=13;	strcpy(id,"+");}
        else if(x=='-'){ token=14;	strcpy(id,"-");}
        else if(x=='*'){ token=15;	strcpy(id,"*");}
        else if(x=='/'){ token=16;	strcpy(id,"/");}
    }
    else if(x==':')
    {
        char next=getc(fp);
        if(next=='=')
        {
            token=18;
            strcpy(id,":=");
        }
        else
        {
            token=17;
            strcpy(id,":");
            ungetc(next,fp);
        }
    }
    else if(x=='>')
    {
        char temp=getc(fp);
        if(temp=='=')
        {
            token=24;
            strcpy(id,">=");
        }
        else
        {
            token=23;
            strcpy(id,">");
            ungetc(temp,fp);
        }
    }
    else if(x=='<')
    {
        char new=getc(fp);
        if(new=='=')
        {
            token=22;
            strcpy(id,"<=");
        }
        else if(new=='>')
        {
            token=21;
            strcpy(id,"<>");
        }
        else
        {
            token=20;
            strcpy(id,"<");
            ungetc(new,fp);
        }
    }
    else if(x=='='||x==';'||x=='('||x==')'||x=='#')
    {
        if(x=='='){  token=25;	strcpy(id,"=");}
        else if(x==';'){ token=26;	strcpy(id,";");}
        else if(x=='('){ token=27;	strcpy(id,"(");}
        else if(x==')'){ token=28;	strcpy(id,")");}
        else if(x=='#'){ token=0;	strcpy(id,"#");}
    }
    else if(x>='0'&&x<='9')
    {
        while(x>='0'&&x<='9')
        {
            num=num*10+x-'0';
            x=getc(fp);
        }
        token=11;
        ungetc(x,fp);
        
    }
    else if((x>='a'&&x<='z')||(x>='A'&&x<='Z'))
    {
        while(((x>='a'&&x<='z')||(x>='A'&&x<='Z'))||(x>='0'&&x<='9'))
        {
            sprintf(id,"%s%c",id,x);
            x=getc(fp);
        }
        token=10;
        ungetc(x,fp);
        if(!strcmp(id,"for"))   token=1;
        else if(!strcmp(id,"if"))   token=2;
        else if(!strcmp(id,"then")) token=3;
        else if(!strcmp(id,"else")) token=4;
        else if(!strcmp(id,"while"))    token=5;
        else if(!strcmp(id,"do"))   token=6;
        
    }
}

int main(int argc, const char *argv[])
{
    if(argc < 2)
    {
        printf("insufficient args\n");
        return 1;
    }
    FILE *fp = fopen(argv[1], "r");
    do
    {
        lexical(fp);
        switch(token)
        {
        case 10:
            printf("(id, %s)\n", id);
            break;
        case 11:
            printf("(num, %d)\n", num);
            break;
        case -1:
            printf("error!\n");
            break;
        default:
            printf("(%d, %s)\n", token, id);
        }
    }
    while(token != 0);


    fclose(fp);
    return 0;
}
